package com.indexingsystem.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.indexingsystem.boss.Vo.FunctionesListVo;
import com.indexingsystem.boss.Vo.PageVo;
import com.indexingsystem.boss.Vo.StatusList;
import com.indexingsystem.boss.Vo.TreeQueryVo;
import com.indexingsystem.boss.admin.mapper.FunctionesMapper;
import com.indexingsystem.boss.admin.mapper.OperationsRelationsMapper;
import com.indexingsystem.boss.admin.mapper.RoleOperationsMapper;
import com.indexingsystem.boss.common.PagedResult;
import com.indexingsystem.boss.entity.Functiones;
import com.indexingsystem.boss.utils.BeanUtils;
import com.indexingsystem.system.service.IFunctionsService;

@Service
public class FunctionsServiceImpl implements IFunctionsService {

	@Resource
	private FunctionesMapper functionsMapper;
	
	@Resource
	private OperationsRelationsMapper operationsRelationsMapper;
	
	@Resource
	private RoleOperationsMapper roleOperationsMapper;

	@Override
	public int deleteByPrimaryKey(String functionId) {
		return functionsMapper.deleteByPrimaryKey(functionId);
	}

	@Override
	public int insertSelective(Functiones record) {
		return functionsMapper.insertSelective(record);
	}

	@Override
	public Functiones selectByPrimaryKey(String functionId) {
		return functionsMapper.selectByPrimaryKey(functionId);
	}

	@Override
	public int updateByPrimaryKeySelective(Functiones record) {
		return functionsMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public PagedResult<Functiones> selectFuncationsList(PageVo pageVo) {
		Integer pageNo = pageVo.getPageNumber() == null ? 1 : pageVo.getPageNumber();
		Integer pageSize = pageVo.getPageSize() == null ? 10 : pageVo.getPageSize();
		// startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
		PageHelper.startPage(pageNo, pageSize);
		return BeanUtils.toPagedResult(functionsMapper.selectFuncationsList(pageVo.getQueryCriteria()));
	}

	@Override
	public int deleteFunctionesByIdBatch(List<String> functionId) throws Exception{
	    int result = 0;
	    if (null != functionId && functionId.size()>0) {
	    	//删除功能
		    result = functionsMapper.deleteFunctionesByIdBatch(functionId);
		    
		    //删除功能操作关系
		    operationsRelationsMapper.deleteOperationsRelationsByFunctionIdBatch(functionId);
		    
		    //删除角色功能操作关系 
		    roleOperationsMapper.deleteRoleOperationsByFunctionIdBatch(functionId);
		}
	    return result;
	}

	/**
	 * 获取功能上下级信息列表.
	 */
	@Override
	public List<FunctionesListVo> getFunctionesList() {
		return functionsMapper.getFunctionesList();
	}

	/**
	 * 获取功能总数量.
	 * 
	 * @return
	 */
	@Override
	public Integer getFunctionesCount(String queryCriteria) {
		return functionsMapper.getFunctionesCount(queryCriteria);
	}

	/**
	 * 通过功能id查询子功能列表信息.
	 * 
	 * @param functionId
	 * @return
	 */
	@Override
	public List<Functiones> getSublevelFunctionsList(String functionId) {
		return functionsMapper.getSublevelFunctionsListById(functionId);
	}

	/**
	 * 获取子功能总数.
	 * 
	 * @param functionId
	 * @return
	 */
	@Override
	public Integer getSublevelFunctionsListSize(TreeQueryVo treeQueryVo) {
		return functionsMapper.getSublevelFunctionsListSize(treeQueryVo);
	}

	/**
	 * 查询子功能分页查询
	 * 
	 * @param functionId
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@Override
	public PagedResult<Functiones> selectSublevelFunctionesList(TreeQueryVo treeQueryVo) {
		Integer pageNo = treeQueryVo.getPageNumber() == null ? 1 : treeQueryVo.getPageNumber();
		Integer pageSize = treeQueryVo.getPageSize() == null ? 10 : treeQueryVo.getPageSize();
		// startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
		PageHelper.startPage(pageNo, pageSize);
		return BeanUtils.toPagedResult(functionsMapper.getSublevelFunctionsList(treeQueryVo));
	}

	/**
	 * 批量启用，禁用功能.
	 * 
	 * @param statusList
	 * @return
	 */
	@Override
	public int batchUpdateInstitutionsWithList(List<StatusList> list)  throws Exception{	    
        int result = 0;	  
	    if(null != list && list.size()>0){
	        result = functionsMapper.batchUpdateInstitutionsWithList(list);
	        //批量修改功能权限关系表
	        operationsRelationsMapper.batchUpdateOptRelationsWithList(list);	        
	        //批量修改角色功能关系表
	        roleOperationsMapper.batchUpdateRoleOperationsWithList(list); 
	        batchUpdateInstitutionsWithList(getSublevelFuncitonesList(list));	               
	    }
		return result;
	}
	
	/**
	 * 获取子功能列表遍历状态.
	 * @param treeQueryVo
	 * @param status
	 * @return
	 */
	private List<StatusList> getSublevelFuncitonesList(List<StatusList> list){
	    List<StatusList> targetStatusList = null;
	    StatusList nodeStatus  = null;
	    if(null != list && list.size()>0){
	        targetStatusList = new ArrayList<StatusList>();
	        for(StatusList statusls:list){                
                TreeQueryVo treeQueryVo = new TreeQueryVo();
                treeQueryVo.setId(statusls.getId());            
                List<Functiones> functionList =  functionsMapper.getSublevelFunctionsList(treeQueryVo); 
                if(null != functionList && functionList.size()>0){                    
                    for(Functiones function:functionList){
                        nodeStatus = new StatusList();
                        nodeStatus.setId(function.getFunctionId());
                        nodeStatus.setStatus(statusls.getStatus());   
                        targetStatusList.add(nodeStatus);                       
                    }
                }             
            }     
	       
	    }
	    return targetStatusList;
	}

	/**
	 * 通过查询功能编码是否已存在
	 * 
	 * @param record
	 * @return
	 */
	@Override
	public List<Functiones> selectFunctionesCodeExits(String  functionCode) {
		return functionsMapper.selectFunctionesCodeExits(functionCode);
	}
	
	   /**
     * 通过查询功能名称是否已存在
     * 
     * @param record
     * @return
     */
    @Override
    public List<Functiones> selectFunctionesNameExits(String functionName) {
        return functionsMapper.selectFunctionesNameExits(functionName);
    }
    
    
    /**
     * 通过查询英文名是否已存在
     * 
     * @param record
     * @return
     */
    @Override
    public List<Functiones> selectFunctionesEnNameExits(String functionEName) {
        return functionsMapper.selectFunctionesEnNameExits(functionEName);
    }
	

	/**
	 * 获取功能上下级权限信息列表.
	 */
	@Override
	public List<FunctionesListVo> getPermissionList() {
		return functionsMapper.getPermissionList();
	}
	
	/**
     * 获取没有被禁用的功能列表.
     * @return
     */
    @Override
    public List<FunctionesListVo> getPermissionEnableList(){
        return functionsMapper.getPermissionEnableList();
    }

	@Override
	public List<FunctionesListVo> getRolePermissionList(String roleId) {
		return functionsMapper.getRolePermissionList(roleId);
	}
}